package com.example.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.result.Result;
import com.example.entity.mes.*;
import com.example.mapper.*;
import com.example.service.SkedService;
import lombok.extern.log4j.Log4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author 14501
* @description 针对表【sked】的数据库操作Service实现
* @createDate 2024-03-13 10:34:52
*/
@Service
public class SkedServiceImpl extends ServiceImpl<SkedMapper, Sked> implements SkedService{

    @Resource
    private SkedMapper skedMapper;
    @Resource
    private MaintainMapper maintainMapper;
    @Resource
    private EquipmentMapper equipmentMapper;
    /**
     * 点检保养项目中间表
     */
    @Resource
    private SkedMaintainMapper skedMaintainMapper;
    /**
     * 设备中间表
     */
    @Resource
    private SkedEquipmentMapper skedEquipmentMapper;

    @Override
    public Result querySkedIPageList(Integer current, Integer size, String code, String name, String type, Integer status) {
        try{
            IPage<Sked> page;
            IPage<Sked> config = new Page<>(current, size);
            // 解决当搜索条件为空时，默认查询所有数据
            QueryWrapper<Sked> maintainQueryWrapper = new QueryWrapper<>();
            if (code!=null&&code!=""){
                maintainQueryWrapper.like("code",code);
            }
            if (name!=null&&name!=""){
                maintainQueryWrapper.eq("name",name);
            }
            if (type!=null&&type!=""){
                maintainQueryWrapper.eq("type",type);
            }
            if (status!=null&&status.equals("")){
                maintainQueryWrapper.eq("status",status);
            }
            page = this.skedMapper.selectPage(config, maintainQueryWrapper);
            // 每页展示的数据
            List<Sked> skedList = page.getRecords();
            // 将响应数据填充到map中
            if (page.getTotal()==0){
                return Result.error("暂无数据");
            }
            Map map = new HashMap();
            map.put("pages", page.getPages());
            map.put("total", page.getTotal());
            map.put("list", skedList);
            return Result.success(map);
        }catch (Exception e) {
            return Result.error("分页查询异常" + String.valueOf(e),"500");
        }
    }

    @Override
    public Result querySkedinById(Long id) {
        try{
            Sked sked = this.skedMapper.selectById(id);
            if (sked==null){
                return Result.error("未查询到数据","500");
            }
            List<SkedEquipment> SkedEquipment = this.skedEquipmentMapper.selectList(new QueryWrapper<SkedEquipment>()
                    .eq("sid", sked.getId()));
            if (SkedEquipment!=null){
                List<Equipment> skedEquipmentList = new ArrayList<>();
                for (SkedEquipment arr:SkedEquipment) {
                    Equipment equipment = this.equipmentMapper.selectById(arr.getEid());
                    if (equipment!=null){
                        skedEquipmentList.add(equipment);
                        sked.setEquipmentList(skedEquipmentList);
                    }
                }
            }
            List<SkedMaintain> skedMaintains = this.skedMaintainMapper.selectList(new QueryWrapper<SkedMaintain>()
                    .eq("sid", sked.getId()));
            if (skedMaintains!=null){
                List<Maintain> skedMaintainList = new ArrayList<>();
                for (SkedMaintain arr:skedMaintains) {
                    Maintain maintain = this.maintainMapper.selectById(arr.getMid());
                    if (maintain!=null){
                        skedMaintainList.add(maintain);
                        sked.setMaintainList(skedMaintainList);
                    }
                }
            }
            return Result.success(sked);
        }catch (Exception e){
            return Result.error("通过id进行查询发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    public Result insertSked(Sked sked) {
        try {
            int insert = this.skedMapper.insert(sked);
            if (insert>0){
                return Result.success("新增成功");
            }else{
                return Result.error("新增失败");
            }
        }catch (Exception e){
            return Result.error("新增异常"+e,"500");
        }
    }

    @Override
    public Result updateSked(Sked sked) {
        try {
            int insert = this.skedMapper.updateById(sked);
            if (insert>0){
                return Result.success();
            }else {
                return Result.error("修改失败","500");
            }
        }catch (Exception e){
            return Result.error("修改发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    public Result deleteSked(Long id) {
        try {
            int i = this.skedMapper.deleteById(id);
            if (i>0){
                return Result.success("删除成功");
            }else{
                return Result.error("删除失败");
            }
        }catch (Exception e){
            return Result.error("删除发生异常"+String.valueOf(e),"500");
        }
    }

    @Override
    @DS("mes")
    @Transactional(rollbackFor = Exception.class) // 批量执行需要加上事务,其中报错,进行回滚操作
    public Result deleteSkedBatch(List<Integer> ids) {
        try {
            // 在多数据源下 此方法需要再Service实现类中的方法上面加上数据源区别注解(@DS（”xxx“）)
            if (removeByIds(ids)) {
                return Result.success("删除成功");
            }
            return Result.error("删除失败","500");
        }catch (Exception e){
            return Result.error("批量删除发生异常"+String.valueOf(e),"500");
        }
    }
}




